package com.bear.newsset.widget;

import android.content.Context;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.bear.newsset.R;
import com.bear.newsset.util.Utils;

/**
 * 菜单View
 */
public class MainMenuView extends RadioGroup {
    public interface OnCheckedChangeListener {
        void onCheckedChanged(MainMenuView view, int checkedId, int position);
    }

    private MainMenuView mView;
    private @DrawableRes int[] icons = null, iconsForSelect = null;
    private int mOldCheckedId, mCheckedId;
    private int mOldPosition, mPosition;

    @Nullable
    private OnCheckedChangeListener mOnCheckedChangeListener;

    public MainMenuView(Context context) {
        super(context);
        init();
    }

    public MainMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        this.mView = this;
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        setOrientation(HORIZONTAL);
        setBackgroundResource(R.drawable.layer_top_line);
    }

    /**
     * 测量
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMode = MeasureSpec.getMode( heightMeasureSpec );
        int width = MeasureSpec.getSize( widthMeasureSpec );
        int height = MeasureSpec.getSize( heightMeasureSpec );
        if( heightMode == MeasureSpec.AT_MOST ) height = Utils.dp2Px(getContext(), 50);
        setMeasuredDimension(width, height);
    }

    /**
     * 布局
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int childWidth = getMeasuredWidth() / getChildCount();
        for (int i = 0; i < getChildCount(); i++) {
            View v = getChildAt( i );
            //View左边的距离
            int vl = childWidth * i;
            //View右边的距离。左边距离 + View实际宽度，第一个Item的距离为View实际宽度
            int vr = ( vr = vl * 2 ) == 0 ? childWidth : vr;
            //设置布局
            v.layout(vl, 0, vr, getMeasuredHeight());
            //设置上下间距
            v.setPadding(0, 10, 0, 10);
        }
    }

    /**
     * 设置item的点击
     */
    private void setItemOnClick() {
        if( icons == null || iconsForSelect == null ) return;
        //默认选中第一个
        setSelectItem( 0 );
        for( int i = 0; i < getChildCount(); i++ ) {
            //设置每一个Item的点击事件
            getChildAt(i).setOnClickListener(v -> {
                //设置选中当前Item的位置
                setSelectItem( (int) v.getTag() );
            });
        }
    }

    /**
     * 创建一个Item
     * @param iconRes   所需的布局
     * @return          {@link ImageView}
     */
    private ImageView createItemView(@DrawableRes int iconRes) {
        ImageView item = new ImageView(getContext());//创建一个item
        LayoutParams lp = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        );
        /* 设置item参数 */
        item.setId( String.valueOf( iconRes ).hashCode() );
        item.setLayoutParams( lp );
        item.setImageResource(iconRes);
        item.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        item.setFocusable(true);
        item.setClickable(true);
        return item;
    }

    /**
     * 选中当前位置的Item
     * @param position  位置
     * @return          {@link MainMenuView}
     */
    public MainMenuView setSelectItem(int position) {
        //设置每一个Item的点击事件
        ImageView item = (ImageView) getChildAt( position );
        mOldCheckedId = mCheckedId;         //临时保存之前选中的Id
        mCheckedId = item.getId();          //选中的Id
        mOldPosition = mPosition;           //临时保存之前选中的位置
        mPosition = (int) item.getTag();    //选中的位置
        //设置图标资源
        item.setImageResource( iconsForSelect[ mPosition ] );
        //重置其他Item的图标
        for( int j = 0; j < getChildCount(); j++ ) {
            if( j != mPosition ) {
                ImageView iv = (ImageView) getChildAt( j );
                iv.setImageResource( icons[ j ] );
            }
        }
        //回调点击改变监听器
        if( mOnCheckedChangeListener != null ) {
            mOnCheckedChangeListener.onCheckedChanged(mView, mCheckedId, mPosition);
        }
        return this;
    }

    /**
     * 获取当前Item的位置
     * @return      位置
     */
    public int getCurrentPosition() { return mPosition; }

    /**
     * 获取之前的Item的位置
     * @return      位置
     */
    public int getOldPosition() { return mOldPosition; }

    /**
     * 获取当前选中Item的Id
     * @return      Id
     */
    public int getCurrentCheckedId() { return mCheckedId; }

    /**
     * 获取之前选中Item的Id
     * @return      Id
     */
    public int getmOldCheckedId() { return mOldCheckedId; }

    /**
     * 设置菜单Item的Icon
     * @param icons     icon的资源
     * @return          {@link MainMenuView}
     */
    public MainMenuView setMenuIcon(@DrawableRes int... icons) {
        ImageView item;
        for( int i = 0; i < icons.length; i++ ) {
            //创建Icon
            item = createItemView( icons[i] );
            //添加到父布局
            addView(item);
            //设置tag，数据为当前下标
            item.setTag(i);
        }
        this.icons = icons;
        setItemOnClick();
        return this;
    }

    /**
     * 设置选中菜单Item的Icon
     * @param iconsForSelect    icon选中的资源
     * @return                  {@link MainMenuView}
     */
    public MainMenuView setMenuIconForSelect(@DrawableRes int... iconsForSelect) {
        this.iconsForSelect = iconsForSelect;
        setItemOnClick();
        return this;
    }

    /**
     * 设置点击发生改变时的事件监听器
     * @param mOnCheckedChangeListener  监听器
     */
    public void setOnCheckedChangeListener(OnCheckedChangeListener mOnCheckedChangeListener) {
        this.mOnCheckedChangeListener = mOnCheckedChangeListener;
    }

}
